package xyz.msj.hrm.web.controller;

import org.springframework.web.bind.annotation.*;
import xyz.msj.hrm.util.AjaxResult;
import xyz.msj.hrm.util.RedisUtils;

import java.util.List;

/**
 * redis缓存控制层
 */
@RestController
public class RedisController {


    //存放====================================================================
    //RedisUtils.INSTANCE.hset("users".getBytes(),"7".getBytes(),SerializeUtil.serialize(new User("zs",7L)));


    //根据ID取User==============================================================

//    byte[] user = RedisUtils.INSTANCE.hget("users".getBytes(),"7".getBytes());
//        System.out.println(SerializeUtil.unserialize(user));
    //取所有值==================================================================

    //根据key获取hash中的所有值 , 然后转换成List封装结果
//    List<byte[]> values = RedisUtils.INSTANCE.hvals("users".getBytes());
//
//    List<User> result  = new ArrayList<>(values.size());	//用来封装结果的list
//
//        values.forEach(value ->{
//        result.add(((User)SerializeUtil.unserialize(value)));
//    });
//        System.out.println(result);






    /**
     * hash 获取所有值
     * http://localhost:1070/redis/set?key=username&value=123
     * @RequestParam  获取地址上面对象的值
     */
    @PostMapping("/redis/hvals")
    public List<byte[]> hvals(@RequestParam String key) {
        try {
            List<byte[]> hvals = RedisUtils.INSTANCE.hvals(key.getBytes());
            return hvals;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * hash 获取值
     * http://localhost:1070/redis/set?key=username&value=123
     * @RequestParam  获取地址上面对象的值
     */
    @PostMapping("/redis/hget")
    public AjaxResult hget(@RequestParam String key,@RequestParam String filed) {
        try {
            byte[] hget = RedisUtils.INSTANCE.hget(key.getBytes(), filed.getBytes());
            return AjaxResult.me().setResultObj(hget);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("促销获取缓存的值失败！请重试");
        }
    }
    /**
     * hash 存储值
     * http://localhost:1070/redis/set?key=username&value=123
     * @RequestParam  获取地址上面对象的值
     */
    @PostMapping("/redis/hset")
    public AjaxResult hset(@RequestParam String key,@RequestParam String filed,@RequestParam byte[] value) {
        try {
            RedisUtils.INSTANCE.hset(key.getBytes(),filed.getBytes(), value);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("促销设置缓存的值失败！请重试");
        }


    }
    /**
     * 设置值
     * http://localhost:1070/redis/set?key=username&value=123
     * @RequestParam  获取地址上面对象的值
     */
    @PostMapping("/redis/set")
    public AjaxResult set(@RequestParam String key,@RequestParam String value) {
        try {
            RedisUtils.INSTANCE.set(key, value);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置缓存的值失败！请重试");
        }


    }
    @PostMapping("/redis/setex")
    public AjaxResult setex(@RequestParam String key,@RequestParam int seconds,@RequestParam String value) {
        try {
            RedisUtils.INSTANCE.setex(key,seconds,value);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置缓存的值失败！请重试");
        }


    }
    /**
     * 获取值
     * http://localhost:1070/redis/get/key
     * @RequestParam  获取地址上面对象的值
     */
    @GetMapping("/redis/get/{key}")
    public AjaxResult get(@PathVariable("key") String key) {
        try {
            String result = RedisUtils.INSTANCE.get(key);
            return AjaxResult.me().setResultObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取缓存的值失败！请重试");
        }


    }


    /**
     * 删除值
     * http://localhost:1070/redis/del/key
     *
     * @RequestParam 获取地址上面对象的值
     */
    @DeleteMapping("/redis/del/{key}")
    public AjaxResult del(@PathVariable("key") String key) {
        //返回删除的结果
        Long delNum = RedisUtils.INSTANCE.del(key);
        //进返回
        return AjaxResult.me().setResultObj(delNum);

    }
}
